package com.fstart.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fstart.common.Result;
import com.fstart.sys.entity.Summarize;
import com.fstart.sys.service.ISummarizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2023-04-23
 */
@RestController
@RequestMapping("/sys/summarize")
public class SummarizeController {
    @Autowired
    public ISummarizeService summarizeService;

    // 更具todoid获取描述信息
    @GetMapping("/{TodoId}")
    public Result<?> getSummarizeByTodoId(@PathVariable Integer TodoId){
        LambdaQueryWrapper<Summarize> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Summarize::getTodoId, TodoId);

        // 更具id查询 描述有则修改，没有则添加
        Summarize byId = summarizeService.getOne(wrapper);

        if(byId == null){
            Summarize summarize = new Summarize();
            summarize.setTitle("暂无描述。。。");

            return Result.success(summarize, "暂无描述");
        }

        return Result.success(byId, "查询完毕");
    }

    // 更新数据
    @PutMapping("/updateSummarize")
    public Result<?> updateSummarize(@RequestBody Summarize summarize){
        // 初始化数据
        summarize.setUpdateTime(LocalDateTime.now());
        LambdaQueryWrapper<Summarize> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Summarize::getTodoId, summarize.getTodoId());

        // 更具id查询 描述有则修改，没有则添加
        Summarize byId = summarizeService.getOne(wrapper);
        if(byId == null){
            summarize.setCreateTime(LocalDateTime.now());
            summarizeService.save(summarize);
        }else{
            summarizeService.update(summarize, wrapper);
        }

        return Result.success("修改成功");
    }
}
